import * as constants from './constants'
import api from '../../../api/api'
import { message } from 'antd';
import Immutable from 'immutable';

const changeAnnounce = (result) => ({
	type: constants.GET_ANNOUNCE,
	value: result
})

const changeAllAnnounce = (result) => ({
	type: constants.CHANGEALLANNOUNCE,
	value: Immutable.fromJS(result)
})

const changeContent = (result) => ({
  type: constants.CHANGECONTENT,
  value: Immutable.fromJS(result)
})

const changeBanner = (result) => ({
  type: constants.CHANGEBANNER,
  value: Immutable.fromJS(result)
})


export const getAnnounce = ()=> {
  return (dispatch)=> {
    api.getAnnounce().then(res => {
      if (res.code === 20000) {
        dispatch(changeAnnounce(res.data.data))
      } else {
        message.error(res.msg);
      }
    })
  }
}

export const getBanner = (params) => {
  return (dispatch) => {
    api.getBanner(params).then(res => {
      if(res.code === 20000) {
        dispatch(changeBanner(res.data.photoUrls.split(',')))
      }else {
        message.error(res.msg);
      }
    })
  }
}


export const getAnnounceById = (params) => (dispatch) => {
  api.getAnnounceById(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeContent(res.data))
      dispatch(getAllAnnounce())
    }else {
      message.error(res.msg);
    }
  })
}

export const getAllAnnounce = () => (dispatch) => {
  api.getAllAnnounce().then(res => {
    if(res.code === 20000) {
      dispatch(changeAllAnnounce(res.data))
    }else {
      message.error(res.msg);
    }
  })
}